/*
 * Yabumi, a 2d scrolling shoot-em-up game.
 * Copyright (C) 2003 Gemma Teruaki <terugen@yabumi.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
 */

#include "video.h"
#include "util.h"

extern _Camera Camera;
_list_header sprite_list_header;

void Sprite_Init()
{
  Create_list(&sprite_list_header);
}

void Sprite_Free()
{
  Free_list(&sprite_list_header);
}

void Sprite_Add(int x, int y, int z, SDL_Surface *image)
{
  _list_item *list_item;
  _Sprite *sprite;
  int a, b, aa, bb;

  a = x - Camera.x;
  b = y - Camera.y;
  aa = DISPLAY_CENTER_X + a;
  bb = DISPLAY_CENTER_Y - b;
  if (!((aa > DISPLAY_X1 - SPRITE_WH) & (bb > DISPLAY_Y1 - SPRITE_WH) &
	(aa < DISPLAY_X2 + SPRITE_WH) & (bb < DISPLAY_Y2 + SPRITE_WH))) return;

  list_item = (_list_item *)pmalloc(sizeof(_list_item));
  Add_item_last(&sprite_list_header, list_item);
  sprite = &list_item->data.Sprite;
  sprite->root = list_item;
  
  sprite->dest.x = aa - (image->w * 0.5);
  sprite->dest.y = bb - (image->h * 0.5);
  sprite->dest.w = image->w;
  sprite->dest.h = image->h;
  sprite->z_order = z;
  sprite->image = image;
}

void Sprite_Add_Ex(int x, int y, int z, SDL_Surface *image)
{
  _list_item *list_item;
  _Sprite *sprite;

  if (!((x > DISPLAY_X1) & (y > DISPLAY_Y1) &
	(x < DISPLAY_X2) & (y < DISPLAY_Y2))) return;

  list_item = (_list_item *)pmalloc(sizeof(_list_item));
  Add_item_last(&sprite_list_header, list_item);
  sprite = &list_item->data.Sprite;
  sprite->root = list_item;
  
  sprite->dest.x = x - (image->w * 0.5);
  sprite->dest.y = y - (image->h * 0.5);
  sprite->dest.w = image->w;
  sprite->dest.h = image->h;
  sprite->z_order = z;
  sprite->image = image;
}
